//
// Created by steven on 2017/2/24.
// Copyright (c) 2017 steven. All rights reserved.
//

#import "GMSettingViewController.h"
#import "GMUserInfoViewController.h"
#import "GMUserAccountViewController.h"
#import "GMNotificationSettingViewController.h"
#import "GMAboutUsViewController.h"
#import "GMOrderListViewController.h"
#import "GMLoginViewController.h"
#import "GMNavigationController.h"
#import "YYLabel+ImagedLabel.h"

#define kProfileTableViewCellIdentifier @"kProfileTableViewCellIdentifier"
@interface GMSettingViewController()<UITableViewDataSource, UITableViewDelegate, GMProfileHeaderViewDelegate>

@property (nonatomic, strong)UIButton *logoutButton;
@property (nonatomic, strong)NSArray *cellTitles;
@end

@implementation GMSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupMainView];
    self.cellTitles = @[@{@"title":@"账号安全",@"icon":@"setting-account-icon"},
                        @{@"title":@"我的订单",@"icon":@"setting-order-icon"},
                        @{@"title":@"推送设置",@"icon":@"setting-notification-icon"},
                        @{@"title":@"关于我们",@"icon":@"setting-info-icon"}];
}

- (void)setupMainView {
    self.navigationItem.title = @"设置";
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.logoutButton];

    [self.tableView autoPinEdgesToSuperviewEdges];
    self.tableView.tableHeaderView = self.headerView;
    self.headerView.avatarImageView.image = [UIImage imageNamed:@"default-user-icon"];
    self.headerView.nameLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];

    [self.logoutButton autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:48];
    [self.logoutButton autoPinEdgeToSuperviewEdge:ALEdgeLeading];
    [self.logoutButton autoPinEdgeToSuperviewEdge:ALEdgeTrailing];
    [self.logoutButton autoSetDimension:ALDimensionHeight toSize:44];
    [self.view bringSubviewToFront:self.logoutButton];
}

- (void)logoutButtonPressed {
    // Clear user info
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"userId"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"token"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"name"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"headImageUrl"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"type"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"vip"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"mobile"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"recommend"];

    GMLoginViewController *loginViewController = [[GMLoginViewController alloc] init];
    GMNavigationController *navigationController = [[GMNavigationController alloc] initWithRootViewController:loginViewController];
    [[[UIApplication sharedApplication] delegate] window].rootViewController = navigationController;
}

- (GMProfileHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[GMProfileHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 244)];
        _headerView.delegate = self;
        _headerView.scoreLabel.text = [NSString stringWithFormat:@"200积分"];
        _headerView.guardLabel.text = [NSString stringWithFormat:@"3守卫"];
        _headerView.subUsersLabel.text = [NSString stringWithFormat:@"2子账户"];
    }
    return _headerView;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (UIButton *)logoutButton {
    if (!_logoutButton) {
        _logoutButton = [[UIButton alloc] init];
        [_logoutButton setTitle:@"退出" forState:UIControlStateNormal];
        [_logoutButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_logoutButton setBackgroundColor:[UIColor whiteColor]];
        [_logoutButton addTarget:self action:@selector(logoutButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    }

    return _logoutButton;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cellTitles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kProfileTableViewCellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    NSString *title = [[self.cellTitles objectAtIndex:indexPath.row] objectForKey:@"title"];
    NSString *icon = [[self.cellTitles objectAtIndex:indexPath.row] objectForKey:@"icon"];
    
    YYLabel *label = (YYLabel *)[cell viewWithTag:1001];
    if (!label) {
        label = [YYLabel labelWithTitle:title fontSize:16 textColor:[UIColor blackColor] imageNamed:icon];
        label.font = [UIFont systemFontOfSize:18];
        label.tag = 1001;
        [cell addSubview: label];
        [label autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
        [label autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:20];
    }

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        GMUserAccountViewController *userAccountViewController = [[GMUserAccountViewController alloc] init];
        [self.navigationController pushViewController:userAccountViewController animated:YES];
    } else if(indexPath.row == 1) {
        GMOrderListViewController *orderViewController = [[GMOrderListViewController alloc] init];
        [self.navigationController pushViewController:orderViewController animated:YES];
    } else if(indexPath.row == 2) {
        GMNotificationSettingViewController *notificationSettingViewController = [[GMNotificationSettingViewController alloc] init];
        [self.navigationController pushViewController:notificationSettingViewController animated:YES];
    } else if(indexPath.row == 3) {
        GMAboutUsViewController *aboutUsViewController = [[GMAboutUsViewController alloc] init];
        [self.navigationController pushViewController:aboutUsViewController animated:YES];
    }
}

#pragma mark - GMProfileHeaderViewDelegate
- (void)avatarImageViewDidTapped {
    GMUserInfoViewController *userInfoViewController = [[GMUserInfoViewController alloc] init];
    [self.navigationController pushViewController:userInfoViewController animated:YES];
}
@end
